package com.echo.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.echo.SportsApplication;
import com.echo.activity.MusicActivity;
import com.echo.activity.SportsActivity;
import com.echo.model.Music;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by EchoGe on 2016/6/11.
 */
public class MusicService extends Service{

    private final static int MODE_INDEX=0,MODE_RANDOM=1,MODE_REPEAT=2;

    private List<Music> mMusicList=new ArrayList<Music>();

    private MediaPlayer mMediaPlayer;

    private OnProgressListener onProgressListener=null;
    private OnMusicChangeListener onMusicChangeListener=null;

    private Thread mPlayThread;

    private boolean mRunService=true;
    private boolean mPlaying=false;

    private int mCurrentMusic=0;
    private int mPlayType=0;//0：首次播放，1：暂停状态

    @Override
    public void onCreate(){
        mPlayThread=new Thread(new Runnable() {
            @Override
            public void run() {
                while(mRunService){
                    while(mPlaying){
                        if(onProgressListener!=null)
                            onProgressListener.onProgress(100*mMediaPlayer.getCurrentPosition()/mMediaPlayer.getDuration());
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        mPlayThread.start();
        mMusicList= MusicActivity.getMusics(SportsApplication.getInstance());
        mMediaPlayer=new MediaPlayer();
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                try {
                    next();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MusicBinder();
    }

    public void setOnProgressListener(OnProgressListener onProgressListener){
        this.onProgressListener=onProgressListener;
    }

    public void setOnMusicChangeListener(OnMusicChangeListener onMusicChangeListener){
        this.onMusicChangeListener=onMusicChangeListener;
    }


    public interface OnProgressListener{
        public void onProgress(int progress);
    }

    public interface OnMusicChangeListener{
        public void onMusicChange(String name);
    }

    public class MusicBinder extends Binder {
        /**
         * 获取当前Service的实例
         * @return
         */
        public MusicService getService(){
            return MusicService.this;
        }
    }

    public void play() throws IOException {
        if(mMediaPlayer.isPlaying()){
            pause();
        }else {
            if (mPlayType == 0) {
                mMediaPlayer.reset();
                mMediaPlayer.setDataSource(mMusicList.get(mCurrentMusic).getPath());
                mMediaPlayer.prepare();
                mMediaPlayer.start();
                mPlayType = 1;
                if(onMusicChangeListener!=null)
                    onMusicChangeListener.onMusicChange(mMusicList.get(mCurrentMusic).getName());
            } else {
                mMediaPlayer.start();
            }
            mPlaying=true;
        }
    }

    public void next() throws IOException {
        if(++mCurrentMusic==mMusicList.size()){
            mCurrentMusic=0;
        }
        mMediaPlayer.reset();
        mMediaPlayer.setDataSource(mMusicList.get(mCurrentMusic).getPath());
        mMediaPlayer.prepare();
        mMediaPlayer.start();
        mPlaying=true;
        if(onMusicChangeListener!=null)
            onMusicChangeListener.onMusicChange(mMusicList.get(mCurrentMusic).getName());
    }

    public void pre() throws IOException {
        if(mCurrentMusic--==0){
            mCurrentMusic=mMusicList.size()-1;
        }
        mMediaPlayer.reset();
        mMediaPlayer.setDataSource(mMusicList.get(mCurrentMusic).getPath());
        mMediaPlayer.prepare();
        mMediaPlayer.start();
        mPlaying=true;
        if(onMusicChangeListener!=null)
            onMusicChangeListener.onMusicChange(mMusicList.get(mCurrentMusic).getName());
    }

    public void pause(){
        mMediaPlayer.pause();
        mPlaying=false;
    }
}
